import json

import requests

if __name__ == "__main__":
    # 1. 指定url
    postURrl = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword"
    for page in range(1, 9):
        # 3. UA伪装
        headers = headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
        }
        # 2. 配置get请求的参数
        data = {
                "cname": "",
                "pid": "",
                "keyword": "北京",
                "pageIndex": str(page),
                "pageSize": "10"
        }
        # 4. 模拟浏览器发送get请求
        response = requests.post(url=postURrl, data=data, headers=headers)
        # 5. 获取text属性响应的字符串数据
        dictText = response.json()
        print(dictText)
        # # 7. 永久存储数据
        fp = open("./kendeji.json", "w", encoding="utf-8")
        # 6. dumps 将字符串数据转换为json格式
        jsonData = json.dump(dictText,fp, ensure_ascii=False)
        print("**********爬取结束************")
